package org.linlinjava.litemall.admin.controller;

import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.annotation.LoginAdminShopId;
import org.linlinjava.litemall.admin.erp.client.DepotClient;
import org.linlinjava.litemall.admin.erp.client.MaterialClient;
import org.linlinjava.litemall.admin.erp.rq.MaterialRQ;
import org.linlinjava.litemall.admin.erp.rs.MaterialRS;
import org.linlinjava.litemall.admin.erp.rs.PageModelRS;
import org.linlinjava.litemall.admin.service.AdminErpService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理erp控制器
 *
 * @author yliyun
 * @description：TODO
 * @date 2022/09/14
 */
@RestController
@RequestMapping("/admin/erp")
public class AdminErpController {
    /**
     * 材料客户
     */
    @Autowired
    private MaterialClient materialClient;
    /**
     * 得宝客户端
     */
    @Autowired
    private DepotClient depotClient;
    /**
     * 管理erp服务
     */
    @Autowired
    private AdminErpService adminErpService;

    /**
     * api名称
     */
    private static final String apiName = "material";

    /**
     * 材料清单url
     */
    @Value("${erp.material.list.url}")
    private String materialListUrl;
    /**
     * 得宝购买url
     */
    @Value("${erp.depot.purchase.url}")
    private String depotPurchaseUrl;
    /**
     * 比尔数量url
     */
    @Value("${erp.depot.billnumber.url}")
    private String billNumberUrl;

    /**
     * 列表
     *
     * @param merchandiseSn 商品sn
     * @param name          名字
     * @param categoryName  类别名称
     * @param shopId        商店id
     * @param page          页面
     * @param limit         限制
     * @return {@link Object}
     */
    @LogAnno
    @GetMapping("/material/list")
    public Object list(String merchandiseSn, String name, String categoryName, @LoginAdminShopId Integer shopId,
                     @RequestParam(defaultValue = "1") Integer page,
                     @RequestParam(defaultValue = "10") Integer limit){
        MaterialRQ rq = new MaterialRQ();
        rq.setName(name);
        rq.setBarCode(merchandiseSn);
        rq.setCategoryName(categoryName);
        rq.setApiName(apiName);
        rq.setCurrentPage(page);
        rq.setPageSize(limit);

        PageModelRS<MaterialRS> materialList = materialClient.getMaterialList(rq, materialListUrl);
        return ResponseUtil.ok(materialList);
    }

    /**
     * 比尔数量
     *
     * @param shopId 商店id
     * @return {@link Object}
     */
    @GetMapping("/depot/billnumber")
    public Object billNumber(@LoginAdminShopId Integer shopId){
        return ResponseUtil.ok(depotClient.getBuildNumber(billNumberUrl));
    }

    /**
     * 同步
     *
     * @return {@link Object}
     */
    @LogAnno
    @GetMapping("/material/sync")
    public Object sync(){
        return adminErpService.sync(apiName, materialListUrl);
    }
}
